package com.gensdai.leliang.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.entity.HomeBean;
import com.gensdai.leliang.utils.BaseUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**首页功能按钮的适配
 * Created by LT on 2017/6/15.
 */

public class BrandListAdapterRec extends DelegateAdapter.Adapter<BrandListAdapterRec.MainViewHolder> {

    private Context mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;

    MenuTypeOnclickListener click;

    List<HomeBean.BrandBean> blist;

    public BrandListAdapterRec(Context context, LayoutHelper layoutHelper, List<HomeBean.BrandBean> list) {
        this(context, layoutHelper, list, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public BrandListAdapterRec(Context context, LayoutHelper layoutHelper, List<HomeBean.BrandBean> list, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.blist = list;
        this.mLayoutParams = layoutParams;
    }

    //设置item的点击回调
    public void setOnMenuTypeClick(MenuTypeOnclickListener click){
        this.click = click;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_content_brand, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, final int position) {
        holder.itemView.setLayoutParams(
                new VirtualLayoutManager.LayoutParams(mLayoutParams));

        final HomeBean.BrandBean menu = blist.get(position);

        //暴露给外部调用
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(click != null){
                    click.OnPositionClick(menu,position);
                }
            }
        });



        if (TextUtils.isEmpty(menu.brandLogo)) {
            Uri uri = Uri.parse("");
            holder.icon.setImageURI(uri);
        }else{
            Uri uri = Uri.parse(menu.brandLogo);
//            holder.icon.setImageURI(uri);
            holder.icon.setController(BaseUtils.resetPic(uri,holder.icon.getController()));
        }

        holder.title.setText(menu.brandName);
    }

    @Override
    public int getItemCount() {
        return blist.size();
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.icon)
        SimpleDraweeView icon;
        @BindView(R.id.title)
        TextView title;

        public MainViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


    /*本适配器的回调接口*/
    public interface MenuTypeOnclickListener<T>{
        void OnPositionClick(T menu, int position);
    }

}
